package com.china08.yxyapi.api.manage.tec.office.cla;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeClass;
import com.china08.yxyapi.entity.mg.office.YxyNoticeClass.YxyNotifictionUser;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeClassRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.JpushForMessageUtil;
import com.china08.yxyapi.util.JpushForNotificationUtil;
import com.china08.yxyapi.util.YxUserPointsUtilV3;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class TecOfficeClaCrtController extends YxyApiBaseController {

    @Autowired
    YxyNoticeClassRepository yxyNoticeClassRepository;

    @Autowired
    private RestTemplate restTemplate;

    @Value(value = "${yx.rest.api.yx.path}")
    String yxyApiYxUrl;

    @Autowired
    YxUserPointsUtilV3 YxUserPointsUtilV3;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/office/class/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        YxyProfile yxyProfile = getProfile(request, response);
        String currentUserId = yxyProfile.getId();
        String schoolId = yxyProfile.getSchoolId();
        String userNick = yxyProfile.getUserNick();
        String avatar = yxyProfile.getUserAvator();
        String userId = yxyProfile.getId();

        List<YxyNoticeClass> yxyNoticeClassLst = new ArrayList<YxyNoticeClass>();

        List<String> classIdLst = reqModel.classIdLst;
        List<String> classNameLst = reqModel.classNameLst;
        YxyNoticeClass yxyNoticeClass = null;

        List<String> studentIdList = new ArrayList<>();

        for (int i = 0; i < classIdLst.size(); i++) {
            List<YxyNotifictionUser> receiver = new ArrayList<YxyNotifictionUser>();
            yxyNoticeClass = new YxyNoticeClass();
            yxyNoticeClass.setSchoolId(schoolId);
            yxyNoticeClass.setTitle(reqModel.title);
            yxyNoticeClass.setContent(reqModel.content);
            yxyNoticeClass.setAttachmentName(reqModel.attachmentName);
            yxyNoticeClass.setAttachmentPath(reqModel.attachmentPath);

            yxyNoticeClass.setClassId(classIdLst.get(i));
            yxyNoticeClass.setSchoolId(schoolId);
            // 查询班级名称
            yxyNoticeClass.setClassNick(classNameLst.get(i));

            yxyNoticeClass.setImage(reqModel.image);
            yxyNoticeClass.setUserId(currentUserId);
            yxyNoticeClass.setNickName(userNick);
            yxyNoticeClass.setAvatar(avatar);
            // 查询班级内的所有班级成员信息
            // FIXED 云校库
            Map<String, List<Map<String, String>>> yxyStudentLstsMap = restTemplate.getForObject(yxyApiYxUrl + "/comm/classStudent/schoolId/" + schoolId + "/getStudentLstByClassId/" + classIdLst.get(i), Map.class);
            List<Map<String, String>> list = yxyStudentLstsMap.get("list");
            for (Map<String, String> map : list) {
                YxyNotifictionUser yxyNotifictionUser = new YxyNoticeClass().new YxyNotifictionUser();
                yxyNotifictionUser.setUserId(map.get("id"));
                yxyNotifictionUser.setUserNick(map.get("name"));
                yxyNotifictionUser.setIsRead(0);
                receiver.add(yxyNotifictionUser);

                studentIdList.add(map.get("id"));
            }
            yxyNoticeClass.setReceiver(receiver);
            yxyNoticeClassLst.add(yxyNoticeClass);
        }
        // FIXED 业务库
        yxyNoticeClassRepository.save(yxyNoticeClassLst);

        // 发送消息
        List<Integer> roleIdList = new ArrayList<>();
        roleIdList.add(1);
        JpushForMessageUtil.sendMessage(2, roleIdList, schoolId, studentIdList, null);

        // 发送通知
        Map<String, String> map = new HashMap<String, String>();
        map.put("modelId", "classNotice");
        List<String> tabAndList1 = new ArrayList<>();
        tabAndList1.add("PARENTS" + schoolId);

        // 推送修改
        JpushForNotificationUtil.sendNotification(reqModel.title, "班级通知", map, studentIdList, tabAndList1);

        // 发送经验值
        YxUserPointsUtilV3.sendNotification(userId);
    }

    public static class ReqModel extends YxyApiReqModel {
        // 班级id list
        public List<String> classIdLst;
        // 班级name list
        public List<String> classNameLst;
        // 标题
        public String title;
        // 内容
        public String content;
        // 图片列表
        public List<String> image;
        // 附件名
        public String attachmentName;
        // 附件路径
        public String attachmentPath;

        @Override
        public void autoCheck() {
            Assert.isTrue(classIdLst.size() > 0, "请选择班级");
            Assert.isTrue(classNameLst.size() > 0, "请选择班级");
            Assert.isTrue(StringUtils.isNotBlank(title), "标题不能为空");
            Assert.isTrue(title.length() > 1 && title.length() < 37, "请输入2-36个字标题");
            Assert.isTrue(StringUtils.isNotBlank(content), "内容不能为空");
            Assert.isTrue(content.length() > 1 && content.length() < 2501, "请输入2-2500个字标题");
        }

    }
}
